{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7173\n"
     ]
    }
   ],
   "source": [
    "import json \n",
    "with open('Dataset/TKY/poi_review_dict_TKY.json', 'r', encoding='utf-8') as f:\n",
    "    poi_data = json.load(f)\n",
    "\n",
    "poiid_list = []\n",
    "for key, poi in poi_data.items():\n",
    "    poiid_list.append(key)\n",
    "\n",
    "print(len(poiid_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# check_in_path = \"NYC_test.csv\"\n",
    "\n",
    "import json\n",
    "from datetime import datetime\n",
    "from collections import defaultdict\n",
    "import pandas as pd\n",
    "\n",
    "users = defaultdict(list)\n",
    "df = pd.read_csv(\"E:\\\\112233\\\\毕设\\\\Dataset\\\\TKY\\\\TKY_train.csv\")\n",
    "\n",
    "for index, row in df.iterrows():\n",
    "    lat = float(row.loc[\"latitude\"])\n",
    "    lng = float(row.loc[\"longitude\"])\n",
    "    if row.loc[\"POI_id\"] not in poiid_list:\n",
    "        continue\n",
    "    record = {\n",
    "        'poi_id': row.loc[\"POI_id\"],\n",
    "        'category_id': row.loc[\"POI_catid\"],\n",
    "        'category_name': row.loc[\"POI_catname\"],\n",
    "        'latitude': lat,\n",
    "        'longitude': lng,\n",
    "        # 'timezone_offset': row.loc[\"UTC_time\"],\n",
    "        'utc_time': row.loc[\"UTC_time\"],\n",
    "    }\n",
    "    users[row.loc[\"user_id\"]].append(record)\n",
    "\n",
    "for user_id, records in users.items():\n",
    "    records.sort(key=lambda x: datetime.strptime(x['utc_time'], \"%Y-%m-%d %H:%M:%S%z\"))\n",
    "\n",
    "with open('user_checkin_TKY.json', 'w', encoding='utf-8') as json_file:\n",
    "    json.dump(dict(users), json_file, indent=4, ensure_ascii=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "check_in_path = \"Dataset//NYC//TKY_train.csv\"\n",
    "\n",
    "import json\n",
    "from datetime import datetime\n",
    "from collections import defaultdict\n",
    "\n",
    "users = defaultdict(list)\n",
    "\n",
    "cat_list = []\n",
    "cat_name_list = []\n",
    "with open(check_in_path, 'r', encoding = \"ISO-8859-1\") as file:\n",
    "    for line in file:\n",
    "        line = line.strip()\n",
    "        parts = line.split('\\t', 7)\n",
    "        user_id, poi_id, cat_id, cat_name, lat, lng, tz_offset, utc_time = parts\n",
    "        # 从签到序列中去除不包含具体数据的poi信息\n",
    "        if poi_id not in poiid_list:\n",
    "            continue    \n",
    "        lat = float(lat)\n",
    "        lng = float(lng)\n",
    "        record = {\n",
    "            'poi_id': poi_id,\n",
    "            'category_id': cat_id,\n",
    "            'category_name': cat_name,\n",
    "            'latitude': lat,\n",
    "            'longitude': lng,\n",
    "            'timezone_offset': tz_offset,\n",
    "            'utc_time': utc_time\n",
    "        }\n",
    "        if cat_name not in cat_name_list :\n",
    "            cat_list.append({cat_name : {\"cat_id\" : cat_id}})\n",
    "            cat_name_list.append(cat_name)\n",
    "        users[user_id].append(record)\n",
    "\n",
    "for user_id, records in users.items():\n",
    "    records.sort(key=lambda x: datetime.strptime(x['utc_time'], '%a %b %d %H:%M:%S %z %Y'))\n",
    "\n",
    "with open('user_checkin.json', 'w', encoding='utf-8') as json_file:\n",
    "    json.dump(dict(users), json_file, indent=4, ensure_ascii=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# label2id = {category: idx for idx, category in enumerate(cat_list)}\n",
    "new_dict = dict()\n",
    "for idx, category in enumerate(cat_list):\n",
    "    for key, value in category.items():\n",
    "        category[key][\"index\"] = idx\n",
    "        new_dict[key] = category[key]\n",
    "\n",
    "# 写入json文件\n",
    "with open('label2id.json', 'w', encoding='utf-8') as json_file:\n",
    "    json.dump(new_dict, json_file, indent=4, ensure_ascii=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "统计完成，结果已保存到 all_user_poi_transitions.json\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "from collections import defaultdict\n",
    "\n",
    "with open('user_checkin_TKY.json', 'r', encoding='utf-8') as f:\n",
    "    user_data = json.load(f)\n",
    "\n",
    "# 双层字典结构：{用户ID: {POI对: 次数}}\n",
    "user_poi_counts = defaultdict(lambda: defaultdict(int))\n",
    "\n",
    "for user_id, records in user_data.items():\n",
    "    if len(records) < 2:\n",
    "        continue\n",
    "    \n",
    "    for i in range(len(records) - 1): \n",
    "        current = records[i]['poi_id']\n",
    "        next_poi = records[i+1]['poi_id']\n",
    "        poi_pair = f\"{current}→{next_poi}\"\n",
    "        user_poi_counts[user_id][poi_pair] += 1\n",
    "\n",
    "final_result = {}\n",
    "for user_id, transitions in user_poi_counts.items():\n",
    "    sorted_transitions = dict(\n",
    "        sorted(transitions.items(), key=lambda x: x[1], reverse=True)\n",
    "    )\n",
    "    final_result[user_id] = sorted_transitions\n",
    "\n",
    "with open('TKY_SFT\\\\all_user_poi_transitions_TKY.json', 'w', encoding='utf-8') as f:\n",
    "    json.dump(final_result, f, indent=4, ensure_ascii=False)\n",
    "\n",
    "print(\"统计完成，结果已保存到 all_user_poi_transitions.json\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "统计完成，结果已保存到 all_poi_transitions.json\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "from collections import defaultdict\n",
    "\n",
    "with open('user_checkin_TKY.json', 'r', encoding='utf-8') as f:\n",
    "    user_data = json.load(f)\n",
    "\n",
    "poi_counter = defaultdict(int)\n",
    "\n",
    "for user_id, records in user_data.items():\n",
    "    for i in range(len(records) - 1 - 2):\n",
    "        current_poi = records[i]['poi_id']\n",
    "        next_poi = records[i+1]['poi_id']\n",
    "        poi_pair = f\"{current_poi}→{next_poi}\"\n",
    "        poi_counter[poi_pair] += 1\n",
    "\n",
    "sorted_pairs = dict(\n",
    "    sorted(poi_counter.items(), key=lambda x: x[1], reverse=True))\n",
    "    \n",
    "with open('TKY_SFT\\\\all_poi_transitions_TKY.json', 'w', encoding='utf-8') as f:\n",
    "    json.dump(sorted_pairs, f, indent=4, ensure_ascii=False)\n",
    "\n",
    "print(\"统计完成，结果已保存到 all_poi_transitions.json\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7173\n"
     ]
    }
   ],
   "source": [
    "# 筛选出有具体信息的POI对\n",
    "import json \n",
    "with open('E:\\\\112233\\\\毕设\\\\Dataset\\\\TKY\\\\POI_review_dict_TKY.json', 'r', encoding='utf-8') as f:\n",
    "    poi_data = json.load(f)\n",
    "\n",
    "poi_array = []\n",
    "\n",
    "for key, value in poi_data.items():\n",
    "    poi_id = key\n",
    "    poi_array.append(poi_id)\n",
    "\n",
    "print(len(poi_array))\n",
    "\n",
    "with open('TKY_SFT\\\\all_poi_transitions_TKY.json', 'r', encoding='utf-8') as f:\n",
    "    poi_transitions = json.load(f)\n",
    "\n",
    "for key in list(poi_transitions.keys()):\n",
    "    current_poi, next_poi = key.split('→')\n",
    "    if current_poi not in poi_array or next_poi not in poi_array:\n",
    "        del poi_transitions[key]\n",
    "\n",
    "with open('TKY_SFT\\\\all_poi_transitions_del_TKY.json', 'w', encoding='utf-8') as f:\n",
    "    json.dump(poi_transitions, f, indent=4, ensure_ascii=False)\n",
    "\n",
    "with open('TKY_SFT\\\\all_user_poi_transitions_TKY.json', 'r', encoding='utf-8') as f:\n",
    "    user_poi_transitions = json.load(f)\n",
    "\n",
    "for user_id, transitions in user_poi_transitions.items():\n",
    "    for key in list(transitions.keys()):\n",
    "        current_poi, next_poi = key.split('→')\n",
    "        if current_poi not in poi_array or next_poi not in poi_array:\n",
    "            del transitions[key]\n",
    "    user_poi_transitions[user_id] = transitions\n",
    "\n",
    "with open('TKY_SFT\\\\all_user_poi_transitions_del_TKY.json', 'w', encoding='utf-8') as f:\n",
    "    json.dump(user_poi_transitions, f, indent=4, ensure_ascii=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 113967/113967 [1:55:03<00:00, 16.51it/s]\n"
     ]
    }
   ],
   "source": [
    "# 计算POI的共现分数\n",
    "\n",
    "import json\n",
    "from tqdm import tqdm\n",
    "\n",
    "with open('TKY_SFT\\\\all_poi_transitions_del_TKY.json', 'r', encoding='utf-8') as f:\n",
    "    poi_transitions = json.load(f)\n",
    "\n",
    "poi_pair_scores = {}\n",
    "# 生成POI转移概率字典\n",
    "for key, value in tqdm(poi_transitions.items()):\n",
    "    current_poi, next_poi = key.split('→')\n",
    "    # 查看user_poitransitions中每个用户\n",
    "    score = 0\n",
    "    for user_id, transitions in user_poi_transitions.items():\n",
    "        # 查看每个用户的POI转移记录\n",
    "        count_key = 0\n",
    "        count_all = 0\n",
    "        for key1 in transitions:\n",
    "            current_poi, next_poi = key1.split('→')\n",
    "            if key == f\"{current_poi}→{next_poi}\":\n",
    "                count_key += transitions[key1]\n",
    "                count_all += transitions[key1]\n",
    "            elif current_poi == current_poi:\n",
    "                count_all += transitions[key1]\n",
    "        if count_all != 0:\n",
    "            score += count_key / count_all\n",
    "    poi_pair_scores[key] = score\n",
    "\n",
    "# 筛选出分数大于0.01小于1的共现对\n",
    "# filtered_poi_pair_scores = {k: v for k, v in poi_pair_scores.items() if v > 0.01 and v < 1}\n",
    "\n",
    "# 保存结果\n",
    "with open('TKY_SFT\\\\TKY_transitions_scores.json', 'w', encoding='utf-8') as f:\n",
    "    json.dump(poi_pair_scores, f, indent=4, ensure_ascii=False)\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('TKY_SFT\\\\TKY_transitions_scores.json', 'r', encoding='utf-8') as f:\n",
    "    poi_scores = json.load(f)\n",
    "\n",
    "# 按照值排序\n",
    "sorted_users = dict(sorted(poi_scores.items(), key=lambda x: x[1], reverse=True))\n",
    "\n",
    "with open('TKY_SFT\\\\TKY_transitions_scores_SORT.json', 'w', encoding='utf-8') as f:\n",
    "    json.dump(sorted_users, f, indent=4, ensure_ascii=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('TKY_SFT\\\\TKY_transitions_scores.json', 'r', encoding='utf-8') as f:\n",
    "    poi_pair_scores = json.load(f)\n",
    "\n",
    "filtered_poi_pair_scores = {k: v for k, v in poi_pair_scores.items() if v > 0.005 and v < 1}\n",
    "\n",
    "with open('TKY_SFT\\\\TKY_train_score.json', 'w', encoding='utf-8') as f:\n",
    "    json.dump(filtered_poi_pair_scores, f, indent=4, ensure_ascii=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "xinao",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
